$(function () {
    merchant = {
        issueCoupons: function (that) {
            var ids = getIds();
            if (!ids) {
                notify.warning("请选择要发放优惠券的商家");
                return false;
            }
            var json = JSON.parse(that.attr('data-json'));
            var url = json.url + '?ids=' + ids.join(',');
            zhiju_show('发放优惠券', url,  json.width, json.height, json.full);
        }
    };

    buttons = {
        preview: function (id, url) {
            zhiju_show('查看', url + '?id=' + id, 900);
        },

        add: function (that) {
            var json = JSON.parse(that.attr('data-json'));
            var url = json.url;
            var full = json.full;
            var title = json.title;
            if (json.is_tab) {
                $("#tpl_" + json.lay_id).html('<iframe src="' + url + '" frameborder="0" style="width: 100%;height: 100%"></iframe>');
                element.tabChange('docDemoTabBrief', json.lay_id);

            } else {
                zhiju_show(title ? title : '新增', json.href, json.width, json.height, full);
            }
        },

        edit: function (id, url, title, uid, w, h, full) {

            zhiju_show(title, url + '&id=' + id + '&uid=' + uid, w, h, full);
        },

        save: function (id, key, val, url) {
            $.ajax({
                type: 'POST',
                url: url,
                data: {id: id, key: key, val: val},
                dataType: "json",
                success: function (data) {
                    if (data.code != 1) {
                        layer.msg(data.msg);
                    }
                }
            });
        },
        remove: function (url, id, obj) {
            layer.confirm('确定要删除吗？', {title: '请确认'}, function (index) {
                layer.load();
                $.ajax({
                    type: 'POST',
                    url: url,
                    data: {id: id},
                    dataType: "json",
                    success: function (data) {
                        if (data.code == 1) {
                            notify.success(data.msg);
                            obj.del();
                        } else {
                            notify.error(data.msg);
                        }
                    }
                });
                layer.closeAll();
            });
        },
        removeAll: function (that, type = 1) {
            //var url = that.attr('data-url');
            var json = JSON.parse(that.attr('data-json'));
            var url = json.href;
            var ids;
            if (type == 2) {
                ids = tableCheck.getData();
            } else {
                ids = getIds();
            }
            if (!ids) {
                notify.warning("请选择要删除的数据");
                return false;
            }
            layer.confirm('确定要删除所选数据吗？', {title: '请确认'}, function (index) {
                layer.load();
                $.ajax({
                    type: 'POST',
                    url: url,
                    data: {id: ids},
                    dataType: "json",
                    success: function (data) {
                        layer.closeAll();
                        if (data.code == 1) {
                            notify.success("操作成功", 1500, function () {
                                location.reload();
                            });
                        } else if(data.code == 301){
                            console.log(data.data.url);
                            zhiju_show(data.data.title, data.data.url, '600', '800');
                        } else {
                            notify.error(data.msg);
                        }
                    }
                });

            });


        },
        //重置密码
        reset: function (that, type) {
            var ids = getIds();
            var json = JSON.parse(that.attr('data-json'));
            if (!ids) {
                notify.warning("请选择要重置密码的数据");
                return false;
            }
            notify.loading("加载中");
            $.ajax({
                type: 'POST',
                url: json.url + '?type=' + type,
                data: {ids: ids},
                dataType: "json",
                success: function (data) {
                    notify.destroyAll()
                    notify.success(data.msg);
                }
            });

        },
        batchAudit: function (that) {
            var ids = getIds();
            if (!ids) {
                notify.warning("请选择要批量操作的数据");
                return false;
            }
            var json = JSON.parse(that.attr('data-json'));

            layer.confirm('确定要批量操作吗？', {title: '请确认'}, function (index) {
                layer.load();
                $.ajax({
                    type: 'POST',
                    url: json.url,
                    data: {ids: ids},
                    dataType: "json",
                    success: function (data) {
                        layer.closeAll();
                        if (data.code == 1) {
                            notify.success(data.message ? data.message : data.msg, 1500, function () {
                                location.reload();
                            });
                        } else {
                            notify.error(data.message ? data.message : data.msg);
                        }
                    }
                });
            });
        },
        exportExcel: function (that) {
            exportApi(that.attr('data-url'), that.attr('data-nav'));
        }
    };

    function getIds(msg) {
        var ids = [];
        var all = getTable().data;
        if (all.length <= 0) {
            return false;
        }
        for (var i = 0; i <= all.length; i++) {
            if (all[i]) {
                ids.push(all[i].id);
            }
        }
        return ids;
    }

    function exportApi(url, title) {
        layui.use(['excel', 'layer'], function () {
            var layer = layui.layer
            var excel = layui.excel
            var load = layer.load(2);
            $.ajax({
                url: url
                , data:form.val("search-form")
                , dataType: 'json'
                , success: function (res) {
                    var data = res.result.data;

                    data.unshift(res.result.title)

                    var timestart = Date.now()
                    excel.exportExcel({
                        sheet1: data
                    }, res.result.name + '.xlsx', 'xlsx')
                    var timeend = Date.now()

                    var spent = (timeend - timestart) / 1000
                    layer.close(load);
                    notify.success('已导出，耗时 ' + spent + ' s');
                }
                , error: function () {
                    layer.close(load);
                    notify.error('获取数据失败！');
                }
            })
        })
    }
})

